package net.ant.watercalc.web.controller;

import com.alibaba.fastjson.JSON;
import net.ant.watercalc.web.pojo.bo.*;
import net.ant.watercalc.web.pojo.vo.ConcentradeResultVo;
import net.ant.watercalc.web.pojo.vo.ListResultVo;
import net.ant.watercalc.web.pojo.vo.RespVo;
import net.ant.watercalc.web.service.CalcService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/calc")
public class CalcController extends BaseController {

    @Autowired
    CalcService calcService;

    @RequestMapping("")
    public String index() {
        return "index";
    }

    @RequestMapping("/result")
    public @ResponseBody
    RespVo getListResults(String anion, String cation, float temperature, float rawph, float adph, String rawOhVal, String adjustOhVal, @RequestParam("adjustCAAN") String adjustCA_AN, float cr, float sr,boolean needAdjust) {

        Anions rawAnion = JSON.parseObject(anion, Anions.class);
        Cations rawCation = JSON.parseObject(cation,Cations.class);
        OHResult rawOHVal = JSON.parseObject(rawOhVal,OHResult.class);
        OHResult adjustOHVal = JSON.parseObject(adjustOhVal,OHResult.class);
        AdjustCAAN adjustCAAN = JSON.parseObject(adjustCA_AN,AdjustCAAN.class);

        ListResult rawResult = calcService.getListResult(rawAnion,rawCation,rawOHVal,temperature,rawph);
        ListResult adjustResult = calcService.getAdjustedListResult(adjustCAAN,rawCation,rawAnion,adjustOHVal,rawOHVal,adph,needAdjust,temperature);

        CoElement coElement = new CoElement();
        Cations conCation = new Cations();
        Anions conAnion = new Anions();
        ListResult concentradeResult  = calcService.getListResultConcentrade(adjustCAAN,rawAnion,rawCation,cr,sr,temperature,adjustResult,coElement,conCation,conAnion);

        ListResultVo listResultVo_1 = new ListResultVo();
        listResultVo_1.setCaption("pH");
        listResultVo_1.setRaw(rawResult.getPh());
        listResultVo_1.setAdjusted(adjustResult.getPh());
        listResultVo_1.setConcentrade(concentradeResult.getPh());

        ListResultVo listResultVo_2 = new ListResultVo();
        listResultVo_2.setCaption("Langelier");
        listResultVo_2.setRaw(rawResult.getLangelier());
        listResultVo_2.setAdjusted(adjustResult.getLangelier());
        listResultVo_2.setConcentrade(concentradeResult.getLangelier());

        ListResultVo listResultVo_3 = new ListResultVo();
        listResultVo_3.setCaption("Stiff & Davis");
        listResultVo_3.setRaw(rawResult.getStiff());
        listResultVo_3.setAdjusted(adjustResult.getStiff());
        listResultVo_3.setConcentrade(concentradeResult.getStiff());

        ListResultVo listResultVo_4 = new ListResultVo();
        listResultVo_4.setCaption("Ionic Strength");
        listResultVo_4.setRaw(rawResult.getIonic());
        listResultVo_4.setAdjusted(adjustResult.getIonic());
        listResultVo_4.setConcentrade(concentradeResult.getIonic());

        ListResultVo listResultVo_5 = new ListResultVo();
        listResultVo_5.setCaption("TDS(mg/l)");
        listResultVo_5.setRaw(rawResult.getTds());
        listResultVo_5.setAdjusted(adjustResult.getTds());
        listResultVo_5.setConcentrade(concentradeResult.getTds());

        ListResultVo listResultVo_6 = new ListResultVo();
        listResultVo_6.setCaption("CaSO4(%Saturation)");
        listResultVo_6.setRaw(rawResult.getCaso4());
        listResultVo_6.setAdjusted(adjustResult.getCaso4());
        listResultVo_6.setConcentrade(concentradeResult.getCaso4());

        ListResultVo listResultVo_7 = new ListResultVo();
        listResultVo_7.setCaption("BaSO4(%Saturation)");
        listResultVo_7.setRaw(rawResult.getBaso4());
        listResultVo_7.setAdjusted(adjustResult.getBaso4());
        listResultVo_7.setConcentrade(concentradeResult.getBaso4());

        ListResultVo listResultVo_8 = new ListResultVo();
        listResultVo_8.setCaption("SrSO4(%Saturation)");
        listResultVo_8.setRaw(rawResult.getSrso4());
        listResultVo_8.setAdjusted(adjustResult.getSrso4());
        listResultVo_8.setConcentrade(concentradeResult.getSrso4());

        ListResultVo listResultVo_9 = new ListResultVo();
        listResultVo_9.setCaption("CaF2(%Saturation)");
        listResultVo_9.setRaw(rawResult.getCaf2());
        listResultVo_9.setAdjusted(adjustResult.getCaf2());
        listResultVo_9.setConcentrade(concentradeResult.getCaf2());

        ListResultVo listResultVo_10 = new ListResultVo();
        listResultVo_10.setCaption("SiO2(%Saturation)");
        listResultVo_10.setRaw(rawResult.getSio2());
        listResultVo_10.setAdjusted(adjustResult.getSio2());
        listResultVo_10.setConcentrade(concentradeResult.getSio2());

        ListResultVo listResultVo_11 = new ListResultVo();
        listResultVo_11.setCaption("CO2(mg/l)");
        listResultVo_11.setRaw(rawResult.getCo2());
        listResultVo_11.setAdjusted(adjustResult.getCo2());
        listResultVo_11.setConcentrade(concentradeResult.getCo2());

        ListResultVo listResultVo_12 = new ListResultVo();
        listResultVo_12.setCaption("CO3(mg/l)");
        listResultVo_12.setRaw(rawResult.getCo3());
        listResultVo_12.setAdjusted(adjustResult.getCo3());
        listResultVo_12.setConcentrade(concentradeResult.getCo3());

        ListResultVo listResultVo_13 = new ListResultVo();
        listResultVo_13.setCaption("HCO3(mg/l)");
        listResultVo_13.setRaw(rawResult.getHco3());
        listResultVo_13.setAdjusted(adjustResult.getHco3());
        listResultVo_13.setConcentrade(concentradeResult.getHco3());

        List<ListResultVo> listResultVos = new ArrayList<>();
        listResultVos.add(listResultVo_1);
        listResultVos.add(listResultVo_2);
        listResultVos.add(listResultVo_3);
        listResultVos.add(listResultVo_4);
        listResultVos.add(listResultVo_5);
        listResultVos.add(listResultVo_6);
        listResultVos.add(listResultVo_7);
        listResultVos.add(listResultVo_8);
        listResultVos.add(listResultVo_9);
        listResultVos.add(listResultVo_10);
        listResultVos.add(listResultVo_11);
        listResultVos.add(listResultVo_12);
        listResultVos.add(listResultVo_13);

        ConcentradeResultVo concentradeResultVo = new ConcentradeResultVo();
        concentradeResultVo.setCoElement(coElement);
        concentradeResultVo.setListResultVoList(listResultVos);
        concentradeResultVo.setConCation(conCation);
        concentradeResultVo.setConAnions(conAnion);
        return writeSuccessResp(concentradeResultVo);
    }

    @PostMapping("/dosingChemical")
    public @ResponseBody
    RespVo getDosingChemical(int tempType, String ph, String rawTemp, @RequestParam("rawResult") String raw) {
        OHResult rawResult = JSON.parseObject(raw, OHResult.class);

        WaterQ waterQ = calcService.getWaterQ(tempType, ph, rawTemp);
        OHResult ohResult = calcService.getOH_Adjusted(rawResult, waterQ);
        return writeSuccessResp(ohResult);
    }


}
